import {createRouter, createWebHashHistory,createWebHistory} from "vue-router"
import Routes from './routes'
import Store from '@/store'
import Cookie from 'js-cookie'
import { removeToken } from "../utils/cookie";
import whiteList from "./whiteList";

const getToken = ()=>{
	if (Store.getters.getToken || Cookie.get('token') ||true) {
		return true
	} else {
		return false
	}
}

const router = createRouter({
	history: createWebHistory(),
	routes: Routes,
	scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      window.scrollTo(0, 0)
    }
  }
})
router.beforeEach((to, from, next) => {
	Store.commit('clearAxiosPromiseCancelArr') // 路由跳转前取消全部请求
	if(getToken()){
		next()
	} else {
		removeToken();
		if(whiteList.includes(to.path)) {
			next()
		} else {
			next('/login')
		}
	}
})

export default router
